<?php
	include_once('connexion.php');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Design by TEMPLATED
http://templated.co
Released for free under the Creative Commons Attribution License

Name       : Assembly 
Description: A two-column, fixed-width design with dark color scheme.
Version    : 1.0
Released   : 20140330

-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Kilbet - The first social betting platform</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="http://fonts.googleapis.com/css?family=Didact+Gothic" rel="stylesheet" />
<link href="styles/default.css" rel="stylesheet" type="text/css" media="all" />
<link href="styles/fonts.css" rel="stylesheet" type="text/css" media="all" />
<link rel="shortcut icon" type="image/x-icon" href="pictures/logo_kilbet.png" />
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script>
	$(function() {
$( "#seeEvents" ).datepicker({
    altField: "#seeEvents",
    closeText: 'Fermer',
    prevText: 'Précédent',
    nextText: 'Suivant',
    currentText: 'Aujourd\'hui',
    monthNames: ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre'],
    monthNamesShort: ['Janv.', 'Févr.', 'Mars', 'Avril', 'Mai', 'Juin', 'Juil.', 'Août', 'Sept.', 'Oct.', 'Nov.', 'Déc.'],
    dayNames: ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'],
    dayNamesShort: ['Dim.', 'Lun.', 'Mar.', 'Mer.', 'Jeu.', 'Ven.', 'Sam.'],
    dayNamesMin: ['D', 'L', 'M', 'M', 'J', 'V', 'S'],
    weekHeader: 'Sem.',
    dateFormat: 'yy/mm/dd',
	firstDay: 1
    });
});
</script>
<!--[if IE 6]><link href="default_ie6.css" rel="stylesheet" type="text/css" /><![endif]-->
</head>

<body>
	<?php include('header.php');?>

<div id="big_content">
<?php
	//Si pas de concours passé en paramètre OU concours inexistant OU user non-connecté et concours privé => pas d'accès à la page
	if(!isset($_GET['contestid']) || isset($_GET['contestid']) && !$contestManager->exist((int) $_GET['contestid']) || !isset($userConnected) && isset($_GET['contestid']) && $contestManager->exist((int) $_GET['contestid']) && $contestManager->get((int) $_GET['contestid'])->privateE()==1) header('Location: index.php');
	
	if(isset($_GET['contestid']))
	{	$contest = $contestManager->get((int) $_GET['contestid']);
		$sport = $sportsManager->get($sportsEventManager->get($eventManager->get($contest->event_id())->sportsEvent_id())->sport_id());
		$event = $eventManager->get($contest->event_id());
	}
	
	//Si user connecté et joueur actif du concours => Accès en mode "participant"
	if(isset($userConnected) && $userManager->getStatusPlayer($userConnected, $contest)[0]==1) $participation = 1;
	else $participation = 0;
	?>
	<div id="contests_menu" class "container">
		<nav id="contests_inside_menu" class="inside_menu">
			<ul>
			<li class="title_inside_menu"><?php echo $contest->name();?></li>
			<li <?php if($current=="/kilbet/contest.php" && (!isset($_GET['view']) || $_GET['view']=="overview")) echo 'class="tab_inside_menu_active"'; else echo 'class=tab_inside_menu';?>><a href="?contestid=<?php echo $contest->id();?>&view=overview">Accueil</a></li>
			<li <?php if($current=="/kilbet/contest.php" && (isset($_GET['view']) && $_GET['view']=="players")) echo 'class="tab_inside_menu_active"'; else echo 'class=tab_inside_menu';?>><a href="?contestid=<?php echo $contest->id();?>&view=players">Classement</a></li>
			<li <?php if($current=="/kilbet/contest.php" && (isset($_GET['view']) && $_GET['view']=="messages")) echo 'class="tab_inside_menu_active"'; else echo 'class=tab_inside_menu';?>><a href="?contestid=<?php echo $contest->id();?>&view=messages">Messages</a></li>
			<li <?php if($current=="/kilbet/contest.php" && (isset($_GET['view']) && $_GET['view']=="events")) echo 'class="tab_inside_menu_active"'; else echo 'class=tab_inside_menu';?>><a href="?contestid=<?php echo $contest->id();?>&view=events&seeEvents=default">Calendrier / Résultats</a></li>
			<?php if($userManager->getStatusPlayer($userConnected, $contest)[0] != 1)
			{
				?>
				<li class="tab_inside_menu"><a href="?contestid=<?php echo $contest->id();?>&view=<?php if(isset($_GET['view'])) echo $_GET['view'];?>&action=inscription" alt="S'inscrire au concours">+</a></li>
				<?php
			}
			?>
			<div style="clear: both;"></div>
			</ul>
		</nav>
		<div style="clear: both;"></div>
		</div>
	<div id="content">
	<section>
	<?php
	if(isset($_GET['view'])) $view = $_GET['view'];
	else $view = "";
	
	if(isset($_GET['sortRankingBy'])) $sortRankingBy = $_GET['sortRankingBy'];
	else $sortRankingBy = "pointsPlayer";
	
	if(isset($_GET['seeEvents'])) $seeEvents = $_GET['seeEvents'];
	else $seeEvents = "results";
	
	if(isset($_GET['sortOrder']) && $_GET['sortOrder']=="desc") $sortOrder = SORT_DESC;
	elseif(isset($_GET['sortOrder']) && $_GET['sortOrder']=="asc") $sortOrder = SORT_ASC;
	else $sortOrder = SORT_DESC;
	
	if(isset($_GET['action']) && $_GET['action']=="inscription") $action = "inscription";
	else $action="";
	
	//Inscription au concours
	if($action=="inscription") 
	{
		$userManager->requestContest($userConnected, $contest);
		$messageInscriptionSuccess = "Bien ouéj mec";
	}
		
	//Vue générale de la page du concours
	if($view=="" || $view=="overview")
	{?>
	<div id="contest_overview">
	
	<div id="contest_presentation">
		<h2><?php echo $contest->name() ?></h2>
			<?php echo $contest->comment() ?>
		<br></br>
	</div>
		
	
	<div id="contest_ranking_overview">
	<?php
		$results = $contestManager->calculateRanking($sport, $contest);
		$rules = $rulesContestManager->get($sport, $contest);
	?>
	<h2>Podium/Mon classement</h2>
	<?php
		switch($sport->id())
		{
			case 1:
				$results = $contestManager->sortRanking($sport, $results, $sortRankingBy, $sortOrder);
				?>
				<table id="podium_board" summary="contest_podium_board" class="tab">
					<thead>
					<tr>
						<th>Position</th>
						<th>Joueur</th>
						<th>Nombre de points</th>
					</tr>
					</thead>
					<?php
					$rank = 0;
					$nbPlayers = 0;
					$meInPodium = 0;
					
					foreach($results as $result)
							{if($nbPlayers<3)
								{	echo '<tr>';
									echo '<td>'.$result['rank'].'</td>';
									echo '<td><a href="profile.php?userid='.$result['player']->id().'" alt="Consulter profil">'.$result['player']->login().'</a></td>';
									echo '<td>'.$result['pointsPlayer'].'</td>';
									echo '</tr>';
									$nbPlayers += 1;
									if($result['player']==$userConnected) $meInPodium =1;
								}
							else break;
							}
					if($meInPodium==0)
					{//créer une fonction getRanking qui retourne le rang d'un user
						echo '<tr>';
						echo '<td>...</td>';
						echo '<td>...</td>';
						echo '<td>...</td>';
						echo '</tr>';
						echo '<tr>';
						echo '<td>'.$result['rank'].'</td>';
						echo '<td><a href="profile.php?userid='.$userConnected->id().'" alt="Consulter profil">'.$userConnected->login().'</a></td>';
						echo '<td>'.$result['pointsPlayer'].'</td>';
						echo '</tr>';
					}
					
					?>
				</table>
				<?php
				break;
			default:
				return;
		}
		?>
	<br></br>
	</div>
	
	<?php
	$yesterday=time()-86400;
	$yesterdayScheduleEvent=$contestManager->getScheduleEvents($sport, $contest, null, date('Y/m/d',$yesterday));
	$today=time();
	$todayScheduleEvent=$contestManager->getScheduleEvents($sport, $contest, null,date('Y/m/d',$today));
	$tomorrow=time()+86400;
	$tomorrowScheduleEvent=$contestManager->getScheduleEvents($sport, $contest, null,date('Y/m/d',$tomorrow));
	
	if($todayScheduleEvent[0]==null)
	{
	?>
	<h2>Aperçu calendrier</h2>
	<div id="yesterday_contest">
	<?php
		if ($yesterdayScheduleEvent[0] != null) 
		{
		
		?>
		<table id="yesterday_board" summary="yesterday_schedule_events_board" class="tab">
			<thead>
			<tr>
				<th>Date</th>
				<th>Tour</th>
				<th>Domicile</th>
				<th>Score domicile</th>
				<th>Score extérieur</th>
				<th>Extérieur</th>
				<th>Points marqués</th>
				<th>Moyenne de tous les joueurs</th>
			</tr>
			</thead>
			<tfoot>
					<tr>
					<td colspan="<?php if(isset($userConnected)) echo '8'; else echo '6';?>">Matchs d'hier</td>
					</tr>
				</tfoot>
			<?php
			foreach ($yesterdayScheduleEvent[0] as $yesterday)
			{
				$pointsUserConnected=$contestManager->getPointsScheduleEvent($sport, $contest, $yesterday, $userConnected, null);
				$pointsAll=$contestManager->getPointsScheduleEvent($sport, $contest, $yesterday, null, null);
				$sumAllPlayers = 0;
				foreach($pointsAll as $pointEachPlayer)
				{
					$sumAllPlayers += $pointEachPlayer[1];
				}
				$listPlayers=$contestManager->getListPlayers($contest, 1);
				$pointsAvg=$sumAllPlayers/count($listPlayers);
				echo
				'<tr>
					<td>'.$yesterday->dateE().'</td>
					<td>'.$yesterday->round().'</td>
					<td>'.$yesterday->homeTeam().'</td>
					<td>'.$yesterday->homeScore().'</td>
					<td>'.$yesterday->awayScore().'</td>
					<td>'.$yesterday->awayTeam().'</td>
					<td>'.$pointsUserConnected[0][1].'</td>
					<td>'.$pointsAvg.'</td>
				</tr>';
			}
			?>
		</table>
		<?php
		} 
		else echo "Aucun match hier";
		
		?>
		</div>
		<?php
		} 
		?>
		<div id="closed_today_contest">
	<?php
		if ($todayScheduleEvent[0] != null) 
		{
		
		?>
		<table id="closed_today_board" summary="today_closed_schedule_events_board" class="tab">
			<thead>
			<tr>
				<th>Date</th>
				<th>Tour</th>
				<th>Domicile</th>
				<th>Score domicile</th>
				<th>Score extérieur</th>
				<th>Extérieur</th>
				<th>Points marqués</th>
				<th>Moyenne de tous les joueurs</th>
			</tr>
			</thead>
			<tfoot>
					<tr>
					<td colspan="<?php if(isset($userConnected)) echo '8'; else echo '6';?>">Matchs du jour terminés</td>
					</tr>
				</tfoot>
			<?php
			foreach ($todayScheduleEvent[0] as $today)
			{
				$pointsUserConnected=$contestManager->getPointsScheduleEvent($sport, $contest, $today, $userConnected, null);
				$pointsAll=$contestManager->getPointsScheduleEvent($sport, $contest, $today, null, null);
				$sumAllPlayers = 0;
				foreach($pointsAll as $pointEachPlayer)
				{
					$sumAllPlayers += $pointEachPlayer[1];
				}
				$listPlayers=$contestManager->getListPlayers($contest, 1);
				$pointsAvg=$sumAllPlayers/count($listPlayers);
				echo
				'<tr>
					<td>'.$today->dateE().'</td>
					<td>'.$today->round().'</td>
					<td>'.$today->homeTeam().'</td>
					<td>'.$today->homeScore().'</td>
					<td>'.$today->awayScore().'</td>
					<td>'.$today->awayTeam().'</td>
					<td>'.$pointsUserConnected[0][1].'</td>
					<td>'.$pointsAvg.'</td>
				</tr>';
			}
			?>
		</table>
		
		<?php
		}		
		else echo "Aucun match terminé aujourd'hui";
		?>
		</div>
		
		<div id="opened_today_contest">
		<?php
		if ($todayScheduleEvent[1] != null) 
		{
		
		?>
		<table id="opened_today_board" summary="today_opened_schedule_events_board" class="tab">
			<thead>
			<tr>
				<th>Date</th>
				<th>Tour</th>
				<th>Domicile</th>
				<th>Victoire domicile</th>
				<th>Victoire extérieur</th>
				<th>Extérieur</th>
				<th>Ecart +10pts</th>
				<th>Prolongations</th>
				<th>Actions</th>
			</tr>
			</thead>
			<tfoot>
					<tr>
					<td colspan="<?php if(isset($userConnected)) echo '9'; else echo '7';?>">Matchs du jour à venir</td>
					</tr>
				</tfoot>
			<?php
			foreach ($todayScheduleEvent[1] as $today)
			{
				echo
				'<tr>
					<td>'.$today->dateE().'</td>
					<td>'.$today->round().'</td>
					<td>'.$today->homeTeam().'</td>
					<td><input type="checkbox"></td>
					<td><input type="checkbox"></td>
					<td>'.$today->awayTeam().'</td>
					<td><input type="checkbox"></td>
					<td><input type="checkbox"></td>
					<td><input type="button">
					<br><input type="button"></td>
				</tr>';
			}
			?>
		</table>
		<?php
		}
		else echo "Aucun match à venir aujourd'hui";
		?>
		</div>
		<?php
		if($todayScheduleEvent[0]!=null)
		{
		?>
		
		<div id="tomorrow_contest">
	<?php
		if ($tomorrowScheduleEvent[1] != null) 
		{
		?>
		<table id="tomorrow_board" summary="tomorrow_schedule_events_board" class="tab">
			<thead>
			<tr>
				<th>Date</th>
				<th>Tour</th>
				<th>Domicile</th>
				<th>Victoire domicile</th>
				<th>Victoire extérieur</th>
				<th>Extérieur</th>
				<th>Ecart +10pts</th>
				<th>Prolongations</th>
				<th>Actions</th>
			</tr>
			</thead>
			<tfoot>
					<tr>
					<td colspan="<?php if(isset($userConnected)) echo '9'; else echo '7';?>">Matchs de demain</td>
					</tr>
				</tfoot>
			<?php
			foreach ($tomorrowScheduleEvent[1] as $tomorrow)
			{
				echo
				'<tr>
					<td>'.$tomorrow->dateE().'</td>
					<td>'.$tomorrow->round().'</td>
					<td>'.$tomorrow->homeTeam().'</td>
					<td><input type="checkbox"></td>
					<td><input type="checkbox"></td>
					<td>'.$tomorrow->awayTeam().'</td>
					<td><input type="checkbox"></td>
					<td><input type="checkbox"></td>
					<td><input type="button">
					<br><input type="button"></td>
				</tr>';
			}
			?>
		</table>
		<?php
		}
		else echo "Aucun match à venir demain";
		?>
		</div>
		<?php
		}
		?>
		
	<h2> <a href="?contestid=<?php echo $contest->id()?>&view=events&seeEvents=default" alt="Calendrier">Voir le calendrier complet</a> </h2>
	</div>
	<?php
	}
	//Vue sur les joueurs du concours
	if($view=="players")
	{
		//Affichage d'un tableau avec le classement du concours
			//Tableau accessible à tous
		$results = $contestManager->calculateRanking($sport, $contest);
		$rules = $rulesContestManager->get($sport, $contest);
		?>
		<div id="section_contest">
		<h2>Classement du concours</h2>
		<?php
		switch($sport->id())
		{
			case 1:
				$results = $contestManager->sortRanking($sport, $results, $sortRankingBy, $sortOrder);
				?>
				<table id="board" summary="contests_board" class="tab">
					<thead>
					<tr>
						<th>Position</th>
						<th>Joueur</th>
						<?php if($rules->winningTeam()==1) {?><th>Points marqués sur<br>"Equipe gagnante"</br><a href="?contestid=<?php echo $contest->id();?>&view=players&sortRankingBy=pointsWinningTeam&sortOrder=asc" alt="Trier"><</a> <a href="?contestid=<?php echo $contest->id();?>&view=players&sortRankingBy=pointsWinningTeam&sortOrder=desc" alt="Trier">></a></th><?php }?>
						<?php if($rules->pointsDifference()>0) {?><th>Points marqués sur<br>"Différence de points"</br><a href="?contestid=<?php echo $contest->id();?>&view=players&sortRankingBy=pointsPointsDifference&sortOrder=asc" alt="Trier"><</a> <a href="?contestid=<?php echo $contest->id();?>&view=players&sortRankingBy=pointsPointsDifference&sortOrder=desc" alt="Trier">></a></th><?php }?>
						<?php if($rules->pointsTotal()>0) {?><th>Points  marqués sur<br>"Nombre de points total"</br><a href="?contestid=<?php echo $contest->id();?>&view=players&sortRankingBy=pointsPointsTotal&sortOrder=asc" alt="Trier"><</a></br> <a href="?contestid=<?php echo $contest->id();?>&view=players&sortRankingBy=pointsPointsTotal&sortOrder=desc" alt="Trier">></a></th><?php }?>
						<?php if($rules->overtime()>0) {?><th>Points marqués sur<br>"Prolongations"</br><a href="?contestid=<?php echo $contest->id();?>&view=players&sortRankingBy=pointsOvertime&sortOrder=asc" alt="Trier"><</a> <a href="?contestid=<?php echo $contest->id();?>&view=players&sortRankingBy=pointsOvertime&sortOrder=desc" alt="Trier">></a></th><?php }?>
						<th>Nombre de points<br><a href="?contestid=<?php echo $contest->id();?>&view=players&sortRankingBy=pointsPlayer&sortOrder=asc" alt="Trier"><</a> <a href="?contestid=<?php echo $contest->id();?>&view=players&sortRankingBy=pointsPlayer&sortOrder=desc" alt="Trier">></a></br></th>
					</tr>
					</thead>
					
					<?php
					$rank = 0;
					foreach($results as $result)
					{
						echo '<tr>';
							echo '<td>'.$result['rank'].'</td>';
							echo '<td><a href="profile.php?userid='.$result['player']->id().'" alt="Consulter profil">'.$result['player']->login().'</a></td>';
							if($rules->winningTeam()==1) echo '<td>'.$result['pointsWinningTeam'].'</td>';
							if($rules->pointsDifference()>0) echo '<td>'.$result['pointsPointsDifference'].'</td>';
							if($rules->pointsTotal()>0) echo '<td>'.$result['pointsPointsTotal'].'</td>';
							if($rules->overtime()>0) echo '<td>'.$result['pointsOvertime'].'</td>';
							echo '<td>'.$result['pointsPlayer'].'</td>';
						echo '</tr>';
					}
					?>
				</table>
				</div>
				<?php
				break;
			default:
				return;
		}
		
		//Possibilité de rechercher des joueurs et les inviter
			//Invitation de joueurs seulement si participation = 1 : moteur de recherche > sélection du joueur > envoi invitation
		if($participation == 1)
		{
		?>
		<br>
		<div id="section_contest">
		<?php
			//Si invitation d'un joueur : on prend en compte l'invitation et on redirige sur la page du contest
			if(isset($_GET['action']) && $_GET['action'] == "invitecontest" && isset($_GET['userid']))
			{
				$userInvited = $userManager->get((int)$_GET['userid']);
				$statusInvited = $userManager->getStatusPlayer($userInvited, $contest);
				if($statusInvited == 0 || $statusInvited == 6) $userManager->inviteContest($userConnected, $userInvited, $contest);
				header('Location: contest.php?contestid='.$contest->id().'&view=players&search');
			}
			echo '<p>';
			echo '<h1>Inviter un joueur</h1>';
			include_once('search_user.php');
			echo '</p>';
			?>
		</div>
		</br>
		<?php
		}
	}
	
	//Vue sur les messages du concours
	if($view=="messages")
	{
	?>
	<div id="contest_wall">
	<?php
		//Affichage du wall seulement si la personne est participante du concours
		if($participation == 1)
		{
			echo '<h2>Mur du concours</h2>';
			//Ecriture d'un nouveau message sur le wall
			?>
			<form method="post" action="contest_post.php" name="new_wall">
				<textarea id="new_wall_message" name="new_wall_message" rows="2" cols="30" onfocus="if(this.value=='Entrez votre message ici') this.value=''" onblur="if(this.value=='') this.value='Entrez votre message ici';" onkeyup="verif(this,2)">Entrez votre message ici</textarea><br />
				<input type="hidden" id="new_wall_contest" name="new_wall_contest" value="<?php echo $contest->id();?>" />
				<input type="submit" id="new_wall_send" name="new_wall_send" value="Ecrire" />
			</form>

			<?php
			//Affichage des messages du wall
			$messages = $contestManager->getListMessages($contest, 0);
			foreach($messages as $message)
			{
				$from = $userManager->get($message->messageFrom());
				//On récupère les éventuelles réponses du post
				$responses = $contestManager->getListMessages($contest, $message->id());
				echo '<div id="block_message" class="message_wall">';
					echo '<p><br />';
					echo '<span class="message_wall_from"><a href="profile.php?userid='.$from->id().'" alt="Consulter le profil">'.$from->login().'</a></span>  ';
					echo '<span class="message_wall_date">('.$message->messageDate().')</span>';
					echo '</p>';
					echo '<p class="message_wall_text">'.$message->text().'</p>';
					foreach($responses as $response)
					{
						$responseFrom = $userManager->get($response->messageFrom());
						echo '<div id="block_response" class="response_message_wall">';
							echo '<p>';
							echo '<span class="response_message_wall_from"><a href="'.$responseFrom->id().'" alt="Consulter le profil">'.$responseFrom->login().'</a></span>  ';
							echo '<span class="response_message_wall_date">('.$response->messageDate().')</span>';
							echo '</p>';
							echo '<p class="response_message_wall_text">'.$response->text().'</p>';
						echo '</div>';
					}
					echo '<form method="post" action="contest_post.php" name="comment_wall">';
						?>
						<textarea id="comment_wall_message" name="comment_wall_message" rows="2" cols="30" onfocus="if(this.value=='Commentez') this.value=''" onblur="if(this.value=='') this.value='Commentez';" onkeyup="verif(this,2)">Commentez</textarea><br />
						<?php
						echo '<input type="hidden" id="comment_wall_contest" name="comment_wall_contest" value="'.$contest->id().'" />';
						echo '<input type="hidden" id="comment_wall_message" name="comment_wall_responseMessage" value="'.$message->id().'" />';
						echo '<input type="submit" id="comment_wall_send" name="comment_wall_send" value="Commenter" />';
						echo '</form>';
				echo '</div>';
			}
		}
		else
		{
			echo '<h1>Le wall du concours n\'est accessible que si vous êtes inscrit au concours.</h1>';
		}
		?>
		</div>
		<?php
	}
	
	//Vue sur les matchs du concours
	if($view=="events")
	{
		if(isset($_GET['seeEvents'])) $seeEvents = $_GET['seeEvents'];
		else $seeEvents = "";
		
		$scheduleToDisplay = array();
		
		switch($seeEvents)
		{
			case "default":
				$titleTabs = array("Les résultats du tour précédent","", "Les résultats du tour en cours", "Les paris à venir du tours en cours", "", "Les paris du prochain tour");
				$typeTabs = array("past", "none", "past", "future", "none", "future");
				$emptyTabs = array("Pas de précédent tour terminé", "", "Aucun match terminé pour le tour en cours", "Aucun résultat à venir pour le tour en cours", "", "Pas de prochain tour prévu");
				$lastRound = $contestManager->getScheduleEvents($sport, $contest, "previous");
				$currentRound = $contestManager->getScheduleEvents($sport, $contest, "current");
				$nextRound = $contestManager->getScheduleEvents($sport, $contest, "next");
				$scheduleToDisplay = array(
					$lastRound[0], $lastRound[1],
					$currentRound[0], $currentRound[1],
					$nextRound[0], $nextRound[1]
					);
				break;
			case "nextRound":
				$titleTabs = array("", "Les paris du prochain tour");
				$emptyTabs = array("", "Pas de prochain tour prévu");
				$typeTabs = array("none", "future");
				$nextRound = $contestManager->getScheduleEvents($sport, $contest, "next");
				$scheduleToDisplay = array(
					$nextRound[0], $nextRound[1]
				);
				break;
			case "previousRound":
				$titleTabs = array("Les résultats du tour précédent", "");
				$emptyTabs = array("Pas de tour précédent terminé", "");
				$typeTabs = array("past", "none");
				$previousRound = $contestManager->getScheduleEvents($sport, $contest, "previous");
				$scheduleToDisplay = array(
					$previousRound[0], $previousRound[1]
				);
				break;
			case "currentRound":
				$titleTabs = array("Les résultats du tour en cours", "Les paris à venir du tour en cours");
				$emptyTabs = array("Aucun match terminé pour le tour en cours", "Aucun résultat à venir pour le tour en cours");
				$typeTabs = array( "past", "future");
				$currentRound = $contestManager->getScheduleEvents($sport, $contest, "current");
				$scheduleToDisplay = array(
					$currentRound[0], $currentRound[1]
				);
				break;
			case "tomorrow":
				$titleTabs = array("", "Les paris de demain");
				$emptyTabs = array("", "Aucun match prévu demain");
				$typeTabs = array("none", "future");
				$demain =  time() + 86400; // ajout de 24 heures
				$tomorrow = $contestManager->getScheduleEvents($sport, $contest, null, date('Y/m/d', $demain));
				$scheduleToDisplay = array(
					$tomorrow[0], $tomorrow[1]
				);
				break;
			case "yesterday":
				$titleTabs = array("Les résultats d'hier", "");
				$emptyTabs = array("Aucun match hier", "");
				$typeTabs = array("past", "none");
				$hier =  time() - 86400; // retrait de 24 heures
				$yesterday = $contestManager->getScheduleEvents($sport, $contest, null, date('Y/m/d', $hier));
				$scheduleToDisplay = array(
					$yesterday[0], $yesterday[1]
				);
				break;
			case "today":
				$typeTabs = array("past", "future");
				$titleTabs = array("Les résultats d'aujourd'hui", "Les paris à venir aujourd'hui");
				$emptyTabs = array("Aucun match terminé aujourd'hui", "Aucun match à venir aujourd'hui");
				$today = $contestManager->getScheduleEvents($sport, $contest, null, date('Y/m/d'));
				$scheduleToDisplay = array(
					$today[0], $today[1]
				);
				break;
			default:
				if(preg_match('`(\d{4})/(\d{2})/(\d{2})`',$seeEvents) == 1)
				{
					$titleTabs = array("Les résultats de ce jour", "Les paris de ce jour");
					$emptyTabs = array("Aucun résultat pour ce jour", "Aucun pari à venir pour ce jour");
					$typeTabs = array("past", "future");
					$day = $contestManager->getScheduleEvents($sport, $contest, null, $seeEvents);
					$scheduleToDisplay = array(
						$day[0], $day[1]
					);
				}
				else
				{
					$titleTabs = array("Les résultats de ce tour", "Les paris de ce tour");
					$emptyTabs = array("Aucun résultat pour ce tour", "Aucun pari à venir pour ce tour");
					$typeTabs = array("past", "future");
					$round = $contestManager->getScheduleEvents($sport, $contest, $seeEvents);
					$scheduleToDisplay = array(
						$round[0], $round[1]
					);
				}
				break;
		}
		
	?>
	<div id="section_contest">
	<form name="changeSchedule" method="POST" action="contest_post.php">
	<p>Jour</p>
	<p>
		<a href="?contestid=<?php echo $contest->id();?>&view=events&seeEvents=yesterday" alt="Hier">Hier</a><br />
		<a href="?contestid=<?php echo $contest->id();?>&view=events&seeEvents=today" alt="Aujourd'hui">Aujourd'hui</a><br />
		<a href="?contestid=<?php echo $contest->id();?>&view=events&seeEvents=tomorrow" alt="Demain">Demain</a><br />
		<input type="hidden" name="contestid" value="<?php echo $contest->id();?>">
		<input type="text" id="seeEvents" name="seeEvents" onChange="this.form.submit();">
	</p>
	<p>Tour</p>
	<p>
		<a href="?contestid=<?php echo $contest->id();?>&view=events&seeEvents=previousRound" alt="Tour précédent">Tour précédent</a><br />
		<a href="?contestid=<?php echo $contest->id();?>&view=events&seeEvents=currentRound" alt="Tour en cours">En cours</a><br />
		<a href="?contestid=<?php echo $contest->id();?>&view=events&seeEvents=nextRound" alt="Prochain tour">Prochain tour</a><br />
		<input type="hidden" name="contestid" value="<?php echo $contest->id();?>">
		<select name="seeEvents2" onChange="this.form.submit();">
			<option value="" selected></option>
			<?php
				$listRounds = $eventManager->getListRounds($sport, $event);
				foreach($listRounds as $round)
				{
					?>
					<option value="<?php echo $round;?>"><?php echo $round;?></option>
					<?php
				}
			?>
		</select>
	</p>
	</form>
		<?php
		/*
		Cas où l'on veut afficher un tableau avec des events passés :
			- si vue default > si le tableau scheduleToDisplay contient plusieurs sous-tableaux ET (que le 1er sous-tableau contient un 1er sous-tableau non-vide OU que le 2e sous-tableau contient un 1er sous-tableau non-vide)
			- si vue previousRound ou currentRound ou round particulier > si le 1er sous-tableau est non-vide
			- si vue yesterday ou today ou jour particulier > si le 1er sous-tableau est non-vide
 		*/
		if(in_array("past",$typeTabs))
		{
			$i=0;
			while(isset($typeTabs[$i]))
			{
				if($typeTabs[$i]=="past")
				{
					?>
					<div id="<?php echo $seeEvents;?>_contest">
						<h2><?php echo $titleTabs[$i];?></h2>
						<table id="past_board" summary="contests_board" class="tab">
							<thead>
							<tr>
								<th>Date</th>
								<th>Tour</th>
								<th>Domicile</th>
								<th>Score domicile</th>
								<th>Score extérieur</th>
								<th>Extérieur</th>
								<th>Résultat pari</th>
								<th>Moyenne de tous les joueurs</th>
								<?php
								//<th>Résultat équipe gagnante</th>
								//<th>Résultat différence score</th>
								//<th>Résultat total points</th>
								//<th>Résultat prolongations</th>
								?>
							</tr>
							</thead>
							<?php
							if(!isset($scheduleToDisplay[$i]) || $scheduleToDisplay[$i] == null)
							{
								echo '
								<tr>
									<td colspan=8>
									'.$emptyTabs[$i].'
									</td>
								</tr>
								';
							}
							else
							{
								foreach($scheduleToDisplay[$i] as $scheduleEvent)
								{		
									echo'
									<tr>
										<td>'.$scheduleEvent->dateE().'</td>
										<td>'.$scheduleEvent->round().'</td>
										<td>'.$scheduleEvent->homeTeam().'</td>
										<td>'.$scheduleEvent->homeScore().'</td>
										<td>'.$scheduleEvent->awayScore().'</td>
										<td>'.$scheduleEvent->awayTeam().'</td>
										<td>test1</td>
										<td>test2</td>
									</tr>
									';
								}
							}
							?>
						</table>
					</div>
					<?php
				}
				$i++;
			}
		}
		/*
		Cas où l'on veut afficher un tableau avec des events à venir :
			- si vue default > si le tableau scheduleToDisplay contient plusieurs sous-tableaux ET (que le 2ème sous-tableau contient un 2ème sous-tableau non-vide OU que le 3ème sous-tableau contient un 2ème sous-tableau non-vide)
			- si vue nextRound ou currentRound ou round particulier > si le 2ème sous-tableau est non-vide
			- si vue tomorrow ou today ou jour particulier > si le 2ème sous-tableau est non-vide
 		*/
		if(in_array("future",$typeTabs))
		{
			?>
			<form name="sendBet" method="POST" action="contest_post.php">
			<input type="submit" name="prevalidateBets" value="Valider"> 
			<input type="submit" name="validateBets" value="Bloquer">
			<?php
			$i=0;
			while(isset($typeTabs[$i]))
			{
				if($typeTabs[$i] == "future")
				{
					?>
					<div id="<?php echo $seeEvents;?>_contest">
						<h2><?php echo $titleTabs[$i];?></h2>
						<table id="future_board" summary="contests_board" class="tab">
							<thead>
							<tr>
								<th>Date</th>
								<th>Tour</th>
								<th>Domicile</th>
								<th>Vainqueur ?</th>
								<th>Vainqueur ?</th>
								<th>Extérieur</th>
								<th>Ecart > 10 points ?</th>
								<th>Prolongations ?</th>
								<th>Actions</th>
							</tr>
							</thead>
							<?php
							if(!isset($scheduleToDisplay[$i]) || $scheduleToDisplay[$i] == null)
							{
								echo '
								<tr>
									<td colspan=9>
									'.$emptyTabs[$i].'
									</td>
								</tr>
								';
							}
							else
							{
								$scheduleEvents = array();
								foreach($scheduleToDisplay[$i] as $scheduleEvent)
								{
									$existingBet = $betsManager->get($sport, null, $scheduleEvent, $contest, $userConnected);
									echo'
									<tr>
										<td>'.$scheduleEvent->dateE().'</td>
										<td>'.$scheduleEvent->round().'</td>
										<td>'.$scheduleEvent->homeTeam().'</td>
										';
									if($existingBet == null) echo '
										<td><input type="radio" name="winningTeam_'.$scheduleEvent->id().'" value="1"></td>
										<td><input type="radio" name="winningTeam_'.$scheduleEvent->id().'" value="2"></td>
										';
									elseif($existingBet != null && $existingBet->status() == 1) 
									{
										echo '<td><input type="radio" name="winningTeam_'.$scheduleEvent->id().'" value="1"';
										if($existingBet->winningTeam() == 1) echo ' checked';
										echo '></td>';
										echo '<td><input type="radio" name="winningTeam_'.$scheduleEvent->id().'" value="2"';
										if($existingBet->winningTeam() == 2) echo ' checked';
										echo '></td>';
									}
									elseif($existingBet != null && $existingBet->status() == 2)
									{
										echo '<td>';
										if($existingBet->winningTeam() == 1) echo 'x';
										echo '</td>';
										echo '<td>';
										if($existingBet->winningTeam() == 2) echo 'x';
										echo '</td>';
									}
									echo '<td>'.$scheduleEvent->awayTeam().'</td>';
									if($existingBet == null) echo '
										<td><input type="checkbox" name="pointsDifference_'.$scheduleEvent->id().'"></td>
										<td><input type="checkbox" name="overtime_'.$scheduleEvent->id().'"></td>
										<td><input type="submit" name="prevalidateBet['.$scheduleEvent->id().']" value="Valider"> <input type="submit" name="validateBet['.$scheduleEvent->id().']" value="Bloquer"></td>
										';
									elseif($existingBet != null && $existingBet->status() == 1) 
									{
										echo '<td><input type="checkbox" name="pointsDifference_'.$scheduleEvent->id().'"';
										if($existingBet->pointsDifference()==1) echo ' checked';
										echo '></td>';
										echo '<td><input type="checkbox" name="overtime_'.$scheduleEvent->id().'"';
										if($existingBet->overtime()==1) echo ' checked';
										echo '></td>';
										echo '<td><input type="submit" name="prevalidateBet['.$scheduleEvent->id().']" value="Valider"> <input type="submit" name="validateBet['.$scheduleEvent->id().']" value="Bloquer"></td>';
									}
									elseif($existingBet != null && $existingBet->status() == 2)
									{
										echo '<td>';
										if($existingBet->pointsDifference()==1) echo 'x';
										echo '</td>';
										echo '<td>';
										if($existingBet->overtime()==1) echo 'x';
										echo '</td>';
										echo '<td></td>';
									}
									$scheduleEvents[] = $scheduleEvent->id();
									echo '</tr>';
								}
							}
							?>
						</table>
					</div>
					<?php
				}
				$i++;
			}
			?>
			<input type="hidden" name="contest" value="<?php echo $contest->id();?>">
			<input type="hidden" name="sport" value="<?php echo $sport->id();?>">
			<input type="hidden" name="scheduleEvents" value="<?php echo serialize($scheduleEvents);?>"> 
			<input type="submit" name="prevalidateBets" value="Valider"> 
			<input type="submit" name="validateBets" value="Bloquer">
			</form>
			<?php
		}
		?>
		</div>
		<?php
	}
	
	?>
	</section>
	</div>

</div>

<?php include_once('footer.php');?>
	
</body>
</html>